import { ListType, ScreenType } from "@/types/interface";
import { request } from "@/utils/request";

import { DataType } from "./type";

export const API_PREFIX = "/admin/sysmenu";

const Api = {
  get: `${API_PREFIX}/querySysMenu`, // 获取列表
  post: `${API_PREFIX}/addSysMenu`, // 新增
  put: `${API_PREFIX}/updateSysMenu`, // 修改
  del: `${API_PREFIX}/deleteSysMenu`, // 删除
  getFile: `${API_PREFIX}/getExcel`, // 导出文件
  getImportTemplate: `${API_PREFIX}/getTemplate`, // 下载模版
  getMenuTreeList: `${API_PREFIX}/querySysMenuTree`, // 获取菜单树
  getMenuTreeListById: `${API_PREFIX}/queryMenuIdByRoleId`, // 根据角色ID查询对应的菜单ID
  postAuthMenu: `${API_PREFIX}/authorizeMenuToRole`, // 角色授权菜单
};

export const getApi: (data: ScreenType) => Promise<ListType<DataType>> = (
  data
) => {
  return request.get({
    url: Api.get,
    params: data,
  });
};

export const postApt = (data: DataType) => {
  return request.post({
    url: Api.post,
    data,
  });
};

export const putApi = (data: DataType) => {
  return request.post({
    url: Api.put,
    data,
  });
};

export const delApi = (data: DataType) => {
  return request.post({
    url: Api.del,
    data,
  });
};

export const getFileApi = (data: ScreenType) => {
  return request.post(
    {
      url: Api.getFile,
      responseType: "blob",
      params: data,
    },
    { isTransformResponse: false }
  );
};

export const getImportTemplateApi = () => {
  return request.post(
    {
      url: Api.getImportTemplate,
      responseType: "blob",
    },
    { isTransformResponse: false }
  );
};
